import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * ExecutorService 实现非结构化并发
 *
 * @author Junki
 * @date 2023-11-14 09:43:25
 */
public class Demo01 {

    private final ExecutorService executorService = Executors.newFixedThreadPool(10);

    Response handle() throws ExecutionException, InterruptedException {
        Future<String> user = executorService.submit(() -> findUser());
        Future<Integer> order = executorService.submit(() -> fetchOrder());
        String theUser = user.get();
        int theOrder = order.get();
        return new Response(theUser, theOrder);
    }

    private String findUser() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
//        int i = 1 / 0;
        System.out.println("findUser执行完毕");
        return "Junki";
    }

    private Integer fetchOrder() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("fetchOrder执行完毕");
        return 60060;
    }

    private record Response(String theUser, int theOrder) {
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Response response = new Demo01().handle();
        System.out.println(response);
    }
}
